package top.superflow.version.detect

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.config.ConfigurableBeanFactory
import org.springframework.context.annotation.Scope
import org.springframework.stereotype.Component
import top.superflow.context.imp.DBContext
import top.superflow.core.metadata.MetadataAble
import top.superflow.version.structure.{AppStructure, DBStructure, Structure}

import scala.collection.mutable
import scala.collection.mutable.ListBuffer


@Component
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
class StructureDetector {

  @Autowired
  private var dbContext : DBContext = _

  def detect: Map[String, Structure] = {
    val structures = mutable.Map[String, Structure]()

    val appStructure = getAppStructure

    structures += (appStructure.getId -> appStructure)

    val dbStructure = getDBStructure

    structures += (dbStructure.getId -> dbStructure)

    structures.toMap
  }

  private def getDBStructure :Structure = {
    val dbPool = dbContext.dbPool.acquire
    val dbSchema = dbPool.getMetadata.getSchema

    new DBStructure(dbSchema)
  }

  private def getAppStructure:Structure = {

    val entityPackages = getAppEntityPackages

    new AppStructure(entityPackages)
  }

  private def getAppEntityPackages : List[String] = {
    val entityPackages = ListBuffer[String]()

    entityPackages += "top.superflow.entity"

    entityPackages.toList
  }
}
